package com.springBoot.springBootSysCore.modules.services.system.impl;

import com.springBoot.springBootSysCore.common.persistence.DataTables;
import com.springBoot.springBootSysCore.common.service.IBaseServiceImpl;
import com.springBoot.springBootSysCore.common.utils.json.JsonUtil;
import com.springBoot.springBootSysCore.common.utils.returnUtil.CodeEnum;
import com.springBoot.springBootSysCore.common.utils.returnUtil.PageUtils;
import com.springBoot.springBootSysCore.common.utils.returnUtil.ResultJsonFormat;
import com.springBoot.springBootSysCore.modules.entity.system.SysFaq;
import com.springBoot.springBootSysCore.modules.entity.system.SysIntro;
import com.springBoot.springBootSysCore.modules.repository.system.SysFaqRepository;
import com.springBoot.springBootSysCore.modules.repository.system.SysIntroRepository;
import com.springBoot.springBootSysCore.modules.services.system.SysFaqService;
import com.springBoot.springBootSysCore.modules.services.system.SysIntroService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Created by timo on 2017/11/27.
 */
@Service
public class SysIntroServiceImpl extends IBaseServiceImpl<SysIntro,String> implements SysIntroService{
    @Autowired
    private SysIntroRepository sysIntroRepository;

    @Override
    @Transactional
    public Map<String, Object> deleteEntity(SysIntro entity) {
        ResultJsonFormat rj = new  ResultJsonFormat("删除成功", CodeEnum.SUCCESS.value());
        try {
            if(StringUtils.isNoneBlank(entity.getId())){
                if(entity.isSureDel()){
//					sysUserRepository.deleteById(user.getId());
                }else{
//					sysUserRepository.updateEntityById(user);
                }
            }else{
                String ids = "";
                String[] a = entity.getIds().split(",");
                for(int i = 0 ; i < a.length ;i++ ){
                    ids += "'"+a[i]+"'";
                    if(i!=a.length-1){
                        ids +=",";
                    }
                }
                if(entity.isSureDel()){
                    sysIntroRepository.deleteByIds(Arrays.asList(a));
                }else{
                    sysIntroRepository.updateDelFlag(Arrays.asList(a));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return rj.convertResultJson();
    }

    @Override
    public Map<String, Object> queryByPage(PageRequest buildPageRequest, SysIntro entity) {
        ResultJsonFormat ret=new ResultJsonFormat(CodeEnum.SUCCESS.value());
        try {
            Page<Object[]> page = sysIntroRepository.findByPage(buildPageRequest);
            String[] li = new String[]{"id","img","objId","sort"};
            List<Map<String, Object>> listMap  = JsonUtil.castEntity(page.getContent(), li);
            ret = new ResultJsonFormat("查询成功", listMap, page.hasNext(), buildPageRequest.getPageNumber()+1, buildPageRequest.getPageSize(), page.getTotalElements(), CodeEnum.SUCCESS.value());
        } catch (Exception e) {
            e.printStackTrace();
            ret.setFlag(CodeEnum.ERROR.value());
            ret.setMsg("系统错误");
        }
        return  ret.convertResultJsonForData();
    }

    @Override
    public DataTables queryByPageForDataTable(HttpServletRequest request,DataTables dt,PageRequest buildPageRequest) {
        Page<Object[]> page = sysIntroRepository.findByPage(buildPageRequest);
        String[] li = new String[]{"id","img","objId","sort"};
        List<Map<String, Object>> listMap  = JsonUtil.castEntity(page.getContent(), li);
        dt.setData(listMap);
        dt.setRecordsFiltered(page.getTotalElements());
        dt.setOtherData(null);
        return dt;
    }

    @Override
    @Transactional
    public Map<String, Object> save(SysIntro entity) {
        ResultJsonFormat rj = new  ResultJsonFormat("保存成功", CodeEnum.SUCCESS.value());
        try {
            //此处检查参数是否规范
            if(StringUtils.isBlank(entity.getId())){
                rj.isAdd();
                entity.preInsert();
                super.saveEntiey(entity);
            }else{
                SysIntro dbe = sysIntroRepository.findOne(entity.getId());
                dbe.preUpdate();
                dbe.EntityCopy(dbe,entity);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return rj.convertResultJson();
    }
}
